import { unitApi } from '@T2/service/api.ts';
export const getMaskOverlay = (area: string, topic: number) => {
  return new Promise((resolve, reject): void => {
    let bdary = new window.BMapGL.Boundary();
    let mapmasks: any = [], borders: any = [];
    bdary.get(area, function (rs: any) {
      for (let i = 0; i < rs.boundaries.length; i++) {
        let xyArr = rs.boundaries[i].split(';');
        let ptArr: any[] = [];
        for (let j = 0; j < xyArr.length; j++) {
          let tmp = xyArr[j].split(',');
          let pt = new window.BMapGL.Point(tmp[0], tmp[1]);
          ptArr.push(pt);
        }
        if (topic === 1) {
          let mapmask = new window.BMapGL.MapMask(ptArr, {
            isBuildingMask: true,
            isPoiMask: true,
            isMapMask: true,
            showRegion: 'inside',
            topFillColor: '#5679ea',
            topFillOpacity: 0.5,
            sideFillColor: '#5679ea',
            sideFillOpacity: 0.9
          });
          mapmasks.push(mapmask);
          let border = new window.BMapGL.Polyline(ptArr, {
            strokeColor: '#4ca7a2',
            strokeWeight: 3,
            strokeOpacity: 1
          });
          borders.push(border);
        }
      }
      resolve({mapmasks, borders})
    });
  });
}
export const loadBMapGLScript = () => {
  return new Promise((resolve, reject): void => {
    window.initialize = function () {
      resolve(true);
    };
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = `//api.map.baidu.com/api?v=1.0&type=webgl&ak=GWhUBquxikUD873z4zE5rACo4e5oKbOL&callback=initialize`;
    script.onerror = reject;
    document.head.appendChild(script);
  });
}
export const loadBMapGLMarkerClustererScript = () => {
  return new Promise((resolve, reject): void => {
    const textIconScript = document.createElement('script');
    textIconScript.type = 'text/javascript';
    textIconScript.src = 'https://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js';
        
    textIconScript.onload = () => {
      const markerClustererScript = document.createElement('script');
      markerClustererScript.type = 'text/javascript';
      markerClustererScript.src = 'https://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js';
  
      // 当第二个脚本加载完成时，resolve Promise
      markerClustererScript.onload = (e) => {
        resolve(e);
      };
  
      // 如果第二个脚本加载失败，reject Promise
      markerClustererScript.onerror = () => {
        reject(new Error('Failed to load MarkerClusterer script.'));
      };
  
      // 添加第二个脚本到页面
      document.head.appendChild(markerClustererScript);
    };
    textIconScript.onerror = () => {
      reject(new Error('Failed to load TextIconOverlay script.'));
    };
  
    // 添加第一个脚本到页面
    document.head.appendChild(textIconScript);
  });
}
export const loadUnitPosition = () => {
  return new Promise((resolve, reject) => {
    unitApi({
      method: 'get',
      params: {},
    }).then((res: any) => {
      let arr = res.results.filter((item: any) => item.x && item.y) || []
      resolve(arr);
    }).catch(e => {
      reject(e);
    });
  });
}